'''
周报自动采集及填写工具
'''
__version__ = '1.1'
__author__ = '废柴'
__date__ = '2021.03.19'

import os 
import sys
import time
import datetime

import qtawesome
from selenium import webdriver
from PyQt5.QtWidgets import (QWidget, QLineEdit, QTextEdit, QLabel, QPushButton, QDialog, QGridLayout, QApplication)


class MainUi(QWidget):
    def __init__(self,parent=None):
        super(MainUi,self).__init__(parent)
        self.initUI()

    def initUI(self):

        self.label_user = QLabel('账号')
        self.label_pwd = QLabel('密码')
        self.login_btn = QPushButton('登入')

        self.user_edit = QLineEdit()
        self.pwd_edit = QLineEdit()
        self.plan_text = QTextEdit()
        self.this_week_text = QTextEdit()
        self.pwd_edit.setEchoMode(QLineEdit.Password)

        self.grid = QGridLayout()
        self.grid.setSpacing(10)

        self.grid.addWidget(self.label_user, 1, 0)
        self.grid.addWidget(self.user_edit, 1, 1)

        self.grid.addWidget(self.label_pwd, 2, 0)
        self.grid.addWidget(self.pwd_edit, 2, 1)

        self.grid.addWidget(self.login_btn, 5, 1)

        self.setLayout(self.grid)

        self.setGeometry(450, 300, 350, 200)
        self.setWindowTitle('周报填写')

        self.login_btn.clicked.connect(self.login_btn_click)
        self.login_btn.clicked.connect(self.close)

    def get_user_pwd(self):
        user_name = self.user_edit.text()
        user_pwd = self.pwd_edit.text()
        return [user_name,user_pwd]

    def login_btn_click(self):
        # 实例化子窗体
        contentPanel = ContentUi()
        user, pwd = self.get_user_pwd()
        content = getJournal(user, pwd)
        contentPanel.user, contentPanel.pwd = user, pwd
        contentPanel.show()
        contentPanel.text_this_week.setText(content)


class ContentUi(QWidget):
    def __init__(self, parent=None):
        super(ContentUi, self).__init__(parent)
        self.contentUI()
        self.user = ''
        self.pwd = '' # user, pwd 用于保存用户名和密码

    def contentUI(self):

        self.setGeometry(450, 200, 450, 400)
        self.setWindowTitle('周报修改')
        self.grid = QGridLayout()
        self.setLayout(self.grid)

        self.label_this_week = QLabel('本\n周\n日\n志')
        self.text_this_week = QTextEdit()

        self.label_plan = QLabel('下\n周\n计\n划')
        self.text_plan = QTextEdit()
        
        self.btn_write = QPushButton('提交')

        self.grid.addWidget(self.label_this_week, 1, 0)
        self.grid.addWidget(self.text_this_week, 1, 1)
        self.grid.addWidget(self.label_plan, 2, 0)
        self.grid.addWidget(self.text_plan, 2, 1)
        self.grid.addWidget(self.btn_write, 3, 1)

        self.btn_write.clicked.connect(self.btn_ok_click)
    
    def btn_ok_click(self):
        # 获取本周日志及下周计划内容并写入
        content = self.text_this_week.toPlainText()
        plan = self.text_plan.toPlainText()
        writeJournal(self.user, self.pwd, content, plan)


def getCurrentWeek():
    monday, sunday = datetime.date.today(),datetime.date.today()
    one_day = datetime.timedelta(days=1)
    while monday.weekday() !=0:
        monday -= one_day
    while sunday.weekday() !=6:
        sunday += one_day
    return monday, sunday


def getJournal(user, pwd):
    
    browser = webdriver.Chrome()

    browser.get('http://ework.gisquest.com:9083/wui/theme/ecology8/page/login.jsp?templateId=23&logintype=1&gopage=&message=&languageid=7')
    browser.find_element_by_name("loginid").send_keys(str(user))
    browser.find_element_by_name("userpassword").send_keys(str(pwd))
    browser.find_element_by_name("submit").click()

    time.sleep(2)

    url = 'http://ework.gisquest.com:9083/formmode/search/CustomSearchBySimpleIframe.jsp?customid=1345&gzrq=$thisweek$&field12101=' + str(user) +'&mainid=0&customTreeDataId=null'
    browser.get(url)
    # html = browser.page_source
    page = browser.find_element_by_css_selector('div.table')
    time.sleep(10)
    content = page.text
    browser.close()
    return content


def writeJournal(user, pwd, content, plan):
    browser = webdriver.Chrome()
    m,s = getCurrentWeek()
    # content = getJournal(user, pwd, browser)
    browser.get('http://devops.gisquest.com/logout')
    browser.find_element_by_name('username').send_keys(str(user))
    browser.find_element_by_name('password').send_keys(str(pwd))
    # browser.find_elements_by_class_name('login-btn').click()
    browser.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[2]/form/ul/li[4]/button').click()
    time.sleep(2)

    url = 'http://devops.gisquest.com/weeklyReport/toWriteWeeklyReport?startDay='+ str(m) +'&endDay='+ str(s) +'&titleName=工作周报'
    browser.get(url)
    time.sleep(5)
    browser.find_element_by_class_name('w-e-text').clear()
    time.sleep(6)    
    browser.find_elements_by_class_name('w-e-text')[0].send_keys(content)
    time.sleep(3)
    browser.find_element_by_xpath('//*[@id="tab-1"]').click()
    browser.find_elements_by_class_name('w-e-text')[1].send_keys(plan)
    time.sleep(4)
    browser.find_element_by_xpath('//*[@id="app"]/div/div[1]/div[2]/div[1]/button[1]/span').click()


def main():
    app = QApplication(sys.argv)
    mainWindow = MainUi()
    mainWindow.show()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()